#include <stdafx.h>
#include "Shaders.h"

int Shaders::Init(char * fileVertexShader, char * fileFragmentShader)
{
	vertexShader = esLoadShader(GL_VERTEX_SHADER, fileVertexShader);

	if ( vertexShader == 0 )
		return -1;

	fragmentShader = esLoadShader(GL_FRAGMENT_SHADER, fileFragmentShader);

	if ( fragmentShader == 0 )
	{
		glDeleteShader( vertexShader );
		return -2;
	}

	program = esLoadProgram(vertexShader, fragmentShader);

	//finding location of uniforms / attributes
	positionAttribute = glGetAttribLocation(program, "a_posL");
	colorAtttribute = glGetAttribLocation(program, "a_color");
	uvAttribute = glGetAttribLocation(program, "a_uv");
	textureUniform = glGetUniformLocation(program, "u_texture");
	matMVPUniform = glGetUniformLocation(program, "u_matMVP");
	// Terrain
	blendUniform = glGetUniformLocation(program, "u_blend");
	texLowUniform = glGetUniformLocation(program, "u_texlow");
	texMedUniform = glGetUniformLocation(program, "u_texmed");
	texHighUniform = glGetUniformLocation(program, "u_texhigh");
	// Reflection
	matWorldUniform = glGetUniformLocation(program, "u_matW");
	cameraPositionUniform = glGetUniformLocation(program, "u_camPos");
	cubeTextureUniform = glGetUniformLocation(program, "u_texture");
	normalAttribute = glGetAttribLocation(program, "a_normL");

	//esLogMessage("UV Loc: %d", uvAttribute);
	
	//texture
	return 0;
}


void Shaders::getAttributeLocation(char* attr_name, GLint& attr_loc)
{
	attr_loc = glGetAttribLocation(program, attr_name);
}
void Shaders::getUniformLocation(char* uni_name, GLint& uni_loc)
{
	uni_loc = glGetUniformLocation(program, uni_name);
}

Shaders::~Shaders()
{
	glDeleteProgram(program);
	glDeleteShader(vertexShader);
	glDeleteShader(fragmentShader);
}